//
//  ViewController.m
//  14_UICollectionView
//
//  Created by 徐昊 on 16/8/2.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"

#define kCollectionViewCellName @"reuse"
#define kCollectionViewReusable @"reuseView"
#define kCollectionViewReusableFooter @"reuseViewFooter"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // UICollectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    NSLog(@"%@", NSStringFromCGSize(layout.itemSize));
    // 1. itemSize 默认大小(50, 50)
//    layout.itemSize = CGSizeMake(120, 120);
    layout.itemSize = CGSizeMake(CGRectGetWidth(self.view.bounds) / 2 - 7.5, 120);
    // 2. 滚动方向
//    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 滚动方向改变, 会交换行和列 (同时也包括区)
    // 3. minimumLineSpacing 最小行间距
    layout.minimumLineSpacing = 5;
    // 4. minimumInteritemSpacing 最小列间距
    layout.minimumInteritemSpacing = 5;
    // 5. sectionInset
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    // 6. 区头大小 横向滚动时, 设置宽; 纵向滚动时, 设置高
    layout.headerReferenceSize = CGSizeMake(200, 200);
    // 7. 区尾大小
    layout.footerReferenceSize = CGSizeMake(200, 200);
    // 8. 区头悬浮效果
    layout.sectionHeadersPinToVisibleBounds = YES;
    // 9. 区尾悬浮效果
    layout.sectionFootersPinToVisibleBounds = YES;
    // UICollectionView 创建
    // 参数1. frame
    // 参数2. 填入不能为空的layout(布局)
    UICollectionView *myCollectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    myCollectionView.delegate = self;
    myCollectionView.dataSource = self;
    [self.view addSubview:myCollectionView];
    [myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewCellName];
    [myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewReusable];
    [myCollectionView registerClass:[UICollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kCollectionViewReusableFooter];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellName forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor redColor];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewReusable forIndexPath:indexPath];
        reusableView.backgroundColor = [UIColor yellowColor];
        return reusableView;
    } else {
        UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kCollectionViewReusableFooter forIndexPath:indexPath];
        reusableView.backgroundColor = [UIColor greenColor];
        return reusableView;
    }
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
